<?php
	$id_jaminan=$_POST['id_jaminan'];
	$id_peserta=$_POST['id'];
	$tgl_input=date("Y-m-d");
	$tgl_mulai=tgl_ind4($_POST['tgl_mulai']);
	$tgl_selesai=tgl_ind4($_POST['tgl_selesai']);
	$jenis=$_POST['jenis'];
	$catatan=$_POST['catatan'];
	$status=$_POST['status'];
	$id_lama=$_POST['id_lama'];
	$chek=$_POST['kunjungan'];
	
	$tgl = date("my02");
	$query = "SELECT max(id_jaminan) as maxID FROM jaminan where id_jaminan like '$tgl%'";
	$kode = mysql_query($query);
	$data = mysql_fetch_array($kode);
	$idMax = $data['maxID'];
	$noUrut = (int) substr($idMax, 6, 4);
	$noUrut++;
	$id_jaminan = "$tgl".sprintf("%04s", $noUrut);
	
	if(isset($_POST['submit'])){
		if($status == 'Baru' && $chek == '0'){
			$sql="insert into jaminan (id_jaminan,id_peserta,tgl_mulai,tgl_selesai,id_jenis_kunjungan, id_catatan_kunjungan,status,id_jaminan_sebelumnya) values ('$id_jaminan','$id_peserta','$tgl_input','$tgl_input',$jenis,$catatan,'$status','')";
		}else if($status == 'Baru' && $chek == ''){
			$sql="insert into jaminan (id_jaminan,id_peserta,tgl_mulai,tgl_selesai,id_jenis_kunjungan, id_catatan_kunjungan,status,id_jaminan_sebelumnya) values ('$id_jaminan','$id_peserta','$tgl_mulai','$tgl_selesai',$jenis,$catatan,'$status','')";
			echo "ab";
		}else if($status == 'Perpanjang' && $chek == '0'){
			$sql="insert into jaminan (id_jaminan,id_peserta,tgl_mulai,tgl_selesai,id_jenis_kunjungan, id_catatan_kunjungan,status,id_jaminan_sebelumnya) values ('$id_jaminan','$id_peserta','$tgl_input','$tgl_input',$jenis,$catatan,'$status','$id_lama')";
			echo "ac";
		}else{
			$sql="insert into jaminan (id_jaminan,id_peserta,tgl_mulai,tgl_selesai,id_jenis_kunjungan, id_catatan_kunjungan,status,id_jaminan_sebelumnya) values ('$id_jaminan','$id_peserta','$tgl_mulai','$tgl_selesai',$jenis,$catatan,'$status','$id_lama')";
			echo "ad";
		}
		$qry=mysql_query($sql);
	}
	
	$url_sukses="module/mod_jaminan/cetak.php?id=$id_jaminan";
	if($qry){
		echo "<meta http-equiv='refresh' content='0; url=$url_sukses'>";
		exit;
	}else{
		echo mysql_error();
	}
?>